//
//  ALViewController.m
//  ALUtils
//
//  Created by anlun on 04/22/2018.
//  Copyright (c) 2018 anlun. All rights reserved.
//

#import "ALViewController.h"
#import "ALImage.h"
#import "UIImage+ALImage.h"
#import "UIImageView+ALImageView.h"

#import "Person.h"
#import "User.h"
#import "NSObject+JSONExtension.h"
#import "Book.h"

#import "ALGPSUtil.h"
#import "ALGPSManager.h"

@interface ALCell : UITableViewCell
@property (nonatomic, strong) UIImageView *imgView;
@end
@implementation ALCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.imgView];
    }
    return self;
}
#pragma mark - get set
-(UIImageView *)imgView{
    if (!_imgView) {
        _imgView=[[UIImageView alloc] initWithFrame:CGRectMake(100, 30, 150, 150)];
        [_imgView setImage:[UIImage imageNamed:@"黑白"]];
//        [_imgView my_cornerRadio:10];
//        _imgView.layer.shadowColor=[UIColor yellowColor].CGColor;
//        _imgView.layer.shadowOpacity=1.0;
//        _imgView.layer.shadowRadius=2.0f;
//        _imgView.layer.shadowOffset=CGSizeMake(-5.0, 5.0);
        [_imgView.layer setShadowPath:CGPathCreateWithRect(CGRectMake(0, 0, 5, 5), NULL)];
    }
    return _imgView;
}
@end

#pragma mark - ALViewController
@interface ALViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *theTableView;
@property (nonatomic, strong) UIImageView *imgView;
@end

@implementation ALViewController

- (void)test:(id)sender{
    [[ALGPSUtil sharedInstance] curLocationCityFinish:^(double latitude, double longitude) {
        NSLog(@"latitude=%@",@(latitude));
        NSLog(@"longitude=%@",@(longitude));
        
        [ALGPSManager getPlacemarkWithLatitude:[NSString stringWithFormat:@"%@",@(latitude)] longitude:[NSString stringWithFormat:@"%@",@(longitude)] closure:^(ALPlacemark *placemark) {
            NSLog(@"province=%@",placemark.province);
            NSLog(@"city=%@",placemark.city);
        }];
    }];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"gps测试" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn setFrame:CGRectMake(100, 200, 150, 80)];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
    
//    [self.view setBackgroundColor:[UIColor grayColor]];
    
//    [self.view addSubview:self.theTableView];
    
    
//    [self.view setBackgroundColor:[UIColor grayColor]];
//    [self.view addSubview:self.imgView];
//    [self.imgView setFrame:CGRectMake(100, 100, 150, 150)];
//    [self.imgView setImage:[UIImage imageNamed:@"黑白"]];
//    [self.imgView my_ImageWithSize:self.imgView.frame.size cornerRadio:10];
    
//    __weak typeof(self) weakSelf = self;
//    UIImage *image = [UIImage imageNamed:@"黑白"];
//    [image my_ImageWithSize:self.imgView.frame.size fillColor:[UIColor whiteColor] cornerRadio:10.0 completion:^(UIImage *image) {
//        weakSelf.imgView.image = image;
//    }];
}

#pragma mark - tableview datasource and delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentify=@"cellIdentify";
    ALCell *theCell=[tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!theCell) {
        theCell=[[ALCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    return theCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 210;
}
-(UITableView *)theTableView{
    if (!_theTableView) {
        _theTableView=[[UITableView alloc] init];
        [_theTableView setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        [_theTableView setDataSource:self];
        [_theTableView setDelegate:self];
    }
    return _theTableView;
}
-(UIImageView *)imgView{
    if (!_imgView) {
        _imgView=[[UIImageView alloc] init];
    }
    return _imgView;
}

/// 字典转模型demo
- (void)json {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"model.json" ofType:nil];
    NSData *jsonData = [NSData dataWithContentsOfFile:path];
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:NULL];
    
    User *user = [User objectWithDict:json];
    Book *book = user.books[0];
    
    NSLog(@"%@",book.name);
}

/// 归解档demo
- (void)archiver {
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"temp.plist"];
    
    //    Person *person = [[Person alloc] init];
    
    // 归档
    //    person.name = @"人人";
    //    [NSKeyedArchiver archiveRootObject:person toFile:path];
    
    // 解档
    Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSLog(@"%@",person.name);
    
    NSLog(@"%@",path);
}

@end
